﻿/*
*本项目中适用的一些工具
*/


var $pf = $pf || {};

/*
*计算Search_Layout布局的页面中(上面是搜索form,下面是grid),grid可以占的高度(便于overflow且用iframe的情况)
*/
$pf.fillSearchPageGridHeight = function (grid) {
    var rect = grid[0].getBoundingClientRect();
    var h = $(window).height() - rect.top - 5;//5为偏差值,否则会多1个滚动条
    grid.height(h);
    return h;
};

$pf.successPopups = function (msg) {
    if (!$pf._successPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'successDialog';
        document.body.appendChild(dialogDiv);

        $pf._successPopups = $('.successDialog').Dialog({
            title: '提示',
            type: 'success',
            autoOpen: false,
            height:150
        });
    }
    $pf._successPopups.Dialog('setMessage', msg);
    $pf._successPopups.Dialog('open');
};
$pf.warningPopups = function (msg) {
    if (!$pf._warningPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'warningDialog';
        document.body.appendChild(dialogDiv);

        $pf._warningPopups = $('.warningDialog').Dialog({
            title: '提示',
            type: 'warning',
            autoOpen: false,
            height:150
        });
        $('.warningDialog .ui-dialog-text').height('100%').css('overflow', 'auto')
        //.find('.patch').height('auto')//.path的作用是使它的next元素相对于它的parent居中
        ;
    }
    $pf._warningPopups.Dialog('setMessage', msg);
    $pf._warningPopups.Dialog('open');
};
$pf.confirmPopups = function (msg, confirmCallback) {
    //debugger;
    if ($pf._confirmPopups) {//confirmCallback里如果有跨作用域的参数，那还是重新初始化比较安全
        $pf._confirmPopups.remove();
        $pf._confirmPopups = null;
    }
    if (!$pf._confirmPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'confirmDialog';
        document.body.appendChild(dialogDiv);

        $pf._confirmPopups = $('.confirmDialog').Dialog({
            title: '确定',
            type: 'confirm',
            autoOpen: false,
            width: 400,
            //height:300,
            confirmCallback: function () {
                //debugger;
                confirmCallback.call(this);
            }
        });
    }
    $pf._confirmPopups.Dialog('setMessage', msg);
    $pf._confirmPopups.Dialog('open');
};
/*
*专卖店详细信息弹窗
*/
$pf.agentPopups = function (agentno) {
    if (!$pf._agentPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'agentDialog';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._agentPopups = $('.agentDialog').Dialog({
            title: '专卖店详细信息',
            autoOpen: false,
            width: 550,
            height: 300
        });
    }
    $('div.agentDialog iframe').attr('src', '/Hyzl/Home/AgentPopups?agentno=' + agentno);
    $pf._agentPopups.Dialog('open');
};

/*
*业务员详细信息弹窗
*/
$pf.ywyzlPopups = function (hybh) {
    if (!$pf._ywyzlPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'ywyzlDialog';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._ywyzlPopups = $('.ywyzlDialog').Dialog({
            title: '业务员详细信息',
            autoOpen: false,
            width: 550,
            height: 300
        });
    }
    $('div.ywyzlDialog iframe').attr('src', '/Hyzl/Home/YWYZLPopups?hybh=' + hybh);
    $pf._ywyzlPopups.Dialog('open');
};

/*
*选择专卖店的弹窗
*事件:rowselected,参数component,row ,row的属性:fgsno,Name...
*/
$pf.selectAgentPopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    if (!$pf._selectAgentPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'selectAgentPopups';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._selectAgentPopups = $('.selectAgentPopups').Dialog({
            title: '选择网点(双击选择一行)',
            autoOpen: autoOpen,
            width: 550,
            height: 520
        });
        $pf._selectAgentPopups.on('rowselected', function () { $pf._selectAgentPopups.Dialog('close'); });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        var sf = openOpts.sf || opts.sf || '';
        var city = openOpts.city || opts.city || '';
        var popupsIframe = $('div.selectAgentPopups iframe');
        popupsIframe.attr('src', '/Hyzl/Home/SelectAgentPopups?sf=' + sf + '&city=' + city);
        $pf._selectAgentPopups.Dialog('open');
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._selectAgentPopups, openPopups: openPopups };
};

/*
*选择产品的弹窗
*事件:rowselected,参数component,row ,row的属性:inv_no,inv_name,invtype...
*/
$pf.selectInvPopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    if (!$pf._selectInvPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'selectInvPopups';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._selectInvPopups = $('.selectInvPopups').Dialog({
            title: '选择产品(双击选择一行)',
            autoOpen: autoOpen,
            width: 800,
            height: 550
        });
        $pf._selectInvPopups.on('rowselected', function () { $pf._selectInvPopups.Dialog('close'); });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        var invType = openOpts.invType || opts.invType || '';
        var popupsIframe = $('div.selectInvPopups iframe');
        popupsIframe.attr('src', '/Hyzl/Home/SelectInvPopups?invType=' + invType);
        $pf._selectInvPopups.Dialog('open');
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._selectInvPopups, openPopups: openPopups };
};

/*
*上传图片的弹窗
*事件:rowselected,参数component,row ,row的属性:fgsno,Name...
*/
$pf.uploadImagePopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    if (!$pf._uploadImagePopups) {
        //debugger;
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'uploadImagePopups';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._uploadImagePopups = $('.uploadImagePopups').Dialog({
            title: '上传图片(可以把图片拖到本窗口)',
            autoOpen: autoOpen,
            width: 900,
            height: 550
        });
        //$pf._uploadImagePopups.on('rowselected', function () { $pf._uploadImagePopups.Dialog('close'); });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        var refUrl = openOpts.refUrl || opts.refUrl || '';
        var popupsIframe = $('div.uploadImagePopups iframe');
        popupsIframe.attr('src', '/Upload/Image?refUrl=' + refUrl);
        $pf._uploadImagePopups.Dialog('open');
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._uploadImagePopups, openPopups: openPopups };
};

/*
*导入Excel的弹窗
*使用方法:$pf.importExcelPopups({templateName:'',templateUrl:'/ImportTemplate/SfPlanMonth.xls',importUrl:''})
*后台参照areas\bonus\controllers\financialdailyreportcontroller-ImportMonth的写法
*事件:uploaded,参数:
*监听器:listeners.uploaded
*/
$pf.importExcelPopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    var templateName = opts.templateName;//不同的名字创建不同的实例
    if ($pf._importExcelPopups === null || $pf._importExcelPopups === undefined) {
        $pf._importExcelPopups = {};
    }
    //debugger;
    if (!$pf._importExcelPopups[templateName]) {
        //$pf._importExcelPopups[templateName]={};
        var templateUrl = opts.templateUrl;//~/ImportTemplate/SfPlanMonth.xls
        var importUrl = opts.importUrl;//
        //debugger;
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'importExcelPopups';
        dialogDiv.innerHTML = 
     '<div class="dialog-content">'+
        '<form class="saper-form ">'+
            '<div class="search-box clearfix">'+
                '<div class="kv-item clearfix">'+
                    '<a href="' + templateUrl + '">下载' + templateName + '模板</a>' +
                '</div>'+
                '<div class="kv-item clearfix">'+
                    '<label for="" style="width:auto">导入' + templateName + '(excel文件)：</label>' +
                    '<div class="kv-item-content">'+
                        '<div class="txt_333_12 file pf-fileupload"><span></span><input type="file" name="importExcelPopupsFileF"><input type="button" value="上传"></div>' +
                    '</div>'+
                '</div>'+
                '<div class="buttons" style="padding-left:0px">'+
                    '<a href="javascript:;" class="sapar-btn sapar-btn-recom ok">确定</a>'+
                 '</div>'+
             '</div>'+
         '</form>'+
     '</div>';
        document.body.appendChild(dialogDiv);

        var $dialogDiv=$(dialogDiv);
        var fileForm = $dialogDiv.find('form.saper-form');
        var fileF = fileForm.find('input[type=file]')
        var okBtn = fileForm.find('a.ok');

        $pf.initFileField(fileF);

        okBtn.click(function () {
            $pf.uploadFile(fileF, importUrl, {
                success: function (data) {
                    if (data.Result === true) {
                        $pf._importExcelPopups[templateName].Dialog('close');
                        $pf.successPopups("导入成功");
                        if (opts.listeners && typeof opts.listeners.uploaded === 'function') {
                            opts.listeners.uploaded();
                        }
                    } else {
                        $pf.warningPopups(data.Message);
                    }
                }
            });
        });

        $pf._importExcelPopups[templateName] = $dialogDiv.Dialog({
            title: '导入' + templateName,
            autoOpen: autoOpen,
            //width: 900,
            //height: 550
            height: 150
        });
        //$pf._importExcelPopups.on('rowselected', function () { $pf._importExcelPopups.Dialog('close'); });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        //var refUrl = openOpts.refUrl || opts.refUrl || '';
        //var popupsIframe = $('div.importExcelPopups iframe');
        //popupsIframe.attr('src', '/Upload/Image?refUrl=' + refUrl);
        $pf._importExcelPopups[templateName].Dialog('open');
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._importExcelPopups[templateName], openPopups: openPopups };
};

/*
*选择显示列(表格显示和打印)
*opts参数:columns 格式string[]
*事件:rowselected,参数component,row ,row的属性:inv_no,inv_name,invtype...
*/
$pf.setVisibleColumnPopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    if (!$pf._setVisibleColumnPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'setVisibleColumnPopups';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._setVisibleColumnPopups = $('.setVisibleColumnPopups').Dialog({
            title: '选择显示列',
            autoOpen: autoOpen,
            width: 800,
            height: 550
        });
        $pf._setVisibleColumnPopups.on('colSetted', function () {
            $pf._setVisibleColumnPopups.Dialog('close');
            $pf.successPopups('设置成功');
        });
        $pf._setVisibleColumnPopups.on('heightChange', function (e,height) {
            $pf._setVisibleColumnPopups.Dialog('setHeight', height + 60);
            $pf._setVisibleColumnPopups.Dialog('setPosition');
        });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        //var columns = openOpts.columns || opts.columns || [];
        var dataAction = openOpts.dataAction || opts.dataAction || [];
        var useLocalCache = openOpts.useLocalCache !== undefined ? openOpts.useLocalCache : opts.useLocalCache;
        var cacheType = openOpts.cacheType || opts.cacheType || [];
        var popupsIframe = $('div.setVisibleColumnPopups iframe');
        //var url = $pf.setUrlParams('/Hyzl/Home/SetVisibleColumnPopups', {
        //    col: columns
        //});
        //popupsIframe.attr('src', url);
        var action = window.location.pathname.replace(/^(\/[^\/]+\/[^\/]+).*$/, '$1' + '/SetVisibleColumnPopups');
        var url = $pf.setUrlParams(action, {
            dataAction: dataAction,
            useLocalCache: useLocalCache,
            cacheType: cacheType
        });
        var layerIdx = null;
        layerIdx = layer.load('正在查询请稍候');
        $pf.post(action, { dataAction: dataAction }, function (data) {
            var doc = popupsIframe[0].contentWindow.document;
            if (doc) {
                doc.open();
                doc.write('')//微软为doc.clear();
                doc.writeln(data);

                doc.close();

                $pf._setVisibleColumnPopups.Dialog('open');
            }
            if (layerIdx) { layer.close(layerIdx); }
        }, 'html');
        //document.getElementById('myIFrame').contentWindow.document.wirte(html);
        //popupsIframe.attr('src', dataAction);
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._setVisibleColumnPopups, openPopups: openPopups };
};

$pf.setGroupColumnPopups = function (opts) {
    opts = opts || {};
    var autoOpen = (opts.autoOpen !== false);
    if (!$pf._setGroupColumnPopups) {
        var dialogDiv = document.createElement("div");
        dialogDiv.className = 'setGroupColumnPopups';
        dialogDiv.innerHTML = '<iframe src="" style="width:100%;height:100%"></iframe>';
        document.body.appendChild(dialogDiv);

        $pf._setGroupColumnPopups = $('.setGroupColumnPopups').Dialog({
            title: '选择汇总列',
            autoOpen: autoOpen,
            width: 800,
            height: 550
        });
        $pf._setGroupColumnPopups.on('colSetted', function () {
            $pf._setGroupColumnPopups.Dialog('close');
            $pf.successPopups('设置成功');
            if (opts.listeners && typeof opts.listeners.colSetted === 'function') {
                opts.listeners.colSetted();
            }
        });
        $pf._setGroupColumnPopups.on('heightChange', function (e, height) {
            $pf._setGroupColumnPopups.Dialog('setHeight', height + 60);
            $pf._setGroupColumnPopups.Dialog('setPosition');
        });
    }
    function openPopups(openOpts) {
        openOpts = openOpts || {};
        //var columns = openOpts.columns || opts.columns || [];
        var dataAction = openOpts.dataAction || opts.dataAction || [];
        var useLocalCache = openOpts.useLocalCache !== undefined ? openOpts.useLocalCache : opts.useLocalCache;
        var cacheType = openOpts.cacheType || opts.cacheType || [];
        //debugger;
        var cacheKey = opts.cacheKey;
        var popupsIframe = $('div.setGroupColumnPopups iframe');
        //var url = $pf.setUrlParams('/Hyzl/Home/SetGroupColumnPopups', {
        //    col: columns
        //});
        //popupsIframe.attr('src', url);
        var action = window.location.pathname.replace(/^(\/[^\/]+\/[^\/]+).*$/, '$1' + '/SetGroupColumnPopups');
        var url = $pf.setUrlParams(action, {
            //dataAction: dataAction,
            useLocalCache: useLocalCache,
            cacheType: cacheType,
            cacheKey: cacheKey
        });
        var layerIdx = null;
        layerIdx = layer.load('正在查询请稍候');
        $pf.post(url, { dataAction: dataAction }, function (data) {
            var doc = popupsIframe[0].contentWindow.document;
            if (doc) {
                doc.open();
                //doc.write('')//微软为doc.clear();
                doc.clear();
                doc.writeln(data);

                doc.close();

                $pf._setGroupColumnPopups.Dialog('open');
            }
            if (layerIdx) { layer.close(layerIdx); }
        }, 'html');
        //document.getElementById('myIFrame').contentWindow.document.wirte(html);
        //popupsIframe.attr('src', dataAction);
    }
    if (autoOpen) { openPopups(); }
    return { popupsApi: $pf._setGroupColumnPopups, openPopups: openPopups };
};